package gat.api.rest.background.backgroundUser;

import java.util.List;

import javax.validation.Valid;

import gat.api.config.annotation.CheckPermissions;
import gat.api.config.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bitsun.core.common.persistence.Pager;
import com.bitsun.core.common.web.ResultDTO;

import gat.api.background.backgroundUser.BRoleApi;
import gat.application.backgroundUser.RoleService;
import gat.dto.req.backgroundUser.RoleReqDto;
import gat.dto.res.backgroundUser.RoleResDto;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 角色信息表
 * @Author: tmac
 * @Email: tmac
 */
@Slf4j
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/role")
public class BRoleApiController implements BRoleApi {


    @Autowired
    private RoleService roleService;

    @Override
    @Log(value = "角色保存")
    @CheckPermissions(value = "role:save")
    @PostMapping(consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<RoleResDto> addRole(
        @ApiParam(value = "要保存的对象", required = true) @Valid @RequestBody RoleReqDto reqDto) {
        RoleResDto resDto = roleService.save(reqDto);
        return ResultDTO.ok(resDto);
    }

    @Override
    @Log(value = "角色删除")
    @CheckPermissions(value = "role:delete")
    @DeleteMapping(value = "/{ids}", produces = {"application/json"})
    public ResultDTO<Void> deleteRole(
        @ApiParam(value = "要删除的对象主键，多个对象主键可以用英文逗号隔开", required = true) @PathVariable("ids") String ids) {
        boolean bl = roleService.deleteByIds(ids);
        return ResultDTO.ok();
    }

    @Override
    @Log(value = "角色列表查询")
    @CheckPermissions(value = "role:list")
    @GetMapping(produces = {"application/json"})
    public ResultDTO<Pager<RoleResDto>> getRoleList(RoleReqDto reqDto) {
        Pager<RoleResDto> resDtoPager = roleService.doPager(reqDto);
        return ResultDTO.ok(resDtoPager);
    }

    @Override
    @GetMapping(value = "/getEnableRoleList", produces = {"application/json"})
    public ResultDTO<List<RoleResDto>> getEnableRoleList() {
        List<RoleResDto> enableRoleList = roleService.getEnableRoleList();
        return ResultDTO.ok(enableRoleList);
    }

    @Override
    @GetMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<RoleResDto> getOneRole(@ApiParam(value = "要查询的对象主键", required = true) @PathVariable("id") Long id) {
        RoleResDto resDto = roleService.getNoChild(id);
        return ResultDTO.ok(resDto);
    }

    @Override
    @Log(value = "角色编辑")
    @CheckPermissions(value = "role:edit")
    @PatchMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<RoleResDto> patchUpdateRole(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id,
                                                 @ApiParam(value = "要修改的对象，对象属性有值的才更新,即null值不更新", required = true) @Valid @RequestBody RoleReqDto reqDto) {
        roleService.updateProps(id, reqDto);
        RoleResDto resDto = roleService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    @Override
    @GetMapping(value = "/getByUserId/{userId}", produces = {"application/json"})
    public ResultDTO<List<RoleResDto>> getByUserId(@ApiParam(value = "用户id", required = true)@PathVariable("userId") Long userId) {
        List<RoleResDto> resDtos = roleService.getByUserId(userId);
        return ResultDTO.ok(resDtos);
    }

}
